﻿using CaseManagerLibrary.Log;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.ServiceProcess;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PSDServer
{
    static class Program
    {

        /// <summary>
        /// 应用程序的主入口点。22222
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                if (Settings.Read())
                {
                    bool runAsService = false;
                    string logtype = "";
                    if (args.Length > 0)
                    {
                        int arg_logtype_index = Array.IndexOf(args, "-l") + 1;
                        logtype = (args.Length > arg_logtype_index ? args[arg_logtype_index] : "");

                        runAsService = !string.IsNullOrEmpty(args.Where(t => t == "--service").FirstOrDefault());
                    }

                    Console.WriteLine("RunAsService=" + runAsService.ToString());

                    PSDServerContext context = new PSDServerContext(logtype, runAsService);

                    Login(Settings.ApiServerAddress, context, runAsService);
                }
            }
            else
            {
                ServiceBase[] ServiceToRun = new ServiceBase[] { new PSDSvrService() };

                ServiceBase.Run(ServiceToRun);
            }
        }

        private static void Login(string address, PSDServerContext context, bool asService)
        {
            //Console.Write("输入用户名:");
            string username = "录像下载服务";//Console.ReadLine();1111

            //Console.Write("输入用户密码:");
            string password = "123456";// Console.ReadLine();

            var result = context.Login(address, username, password);
            if (result.success)
            {
                context.Init();

                if (asService)
                {
                    RunAsService(context);
                }
                else
                {
                    context.Logger.MC();
                    Run(context);
                }
            }
            else
            {
                context.Logger.Error(result.message);
                Login(address, context, asService);
            }
        }

        private static void Run(PSDServerContext context)
        {
            var cmd = Console.ReadLine();
            while (cmd != "exit")
            {
                switch (cmd)
                {
                    case "start":
                        if (!context.Working)
                        {
                            context.Start();
                        }
                        break;
                    case "stop":
                        if (context.Working)
                        {
                            context.Stop();
                        }
                        break;
                    case "device-state":
                        context.Logger.Text("PSD List:");
                        context.Logger.Text("");
                        foreach (var ds in context.DeviceList)
                        {
                            context.Logger.Text(ds.DeviceId + "  " + ds.DeviceInfo.device_name + "  " + ds.DeviceInfo.device_ip
                                + "  " + (ds.Online ? "online" : "offline")
                                + "  " + (ds.LastHeertBeatTime == null ? "" : ds.LastHeertBeatTime.Value.ToString("yyyy-MM-dd HH:mm:ss"))
                                + "  " + (ds.DeviceInfo.main_storage_total > 0 ? (ds.DeviceInfo.main_storage_free / ds.DeviceInfo.main_storage_total * 100).ToString("#.00") + "%" : "")
                                + "  " + (ds.DeviceInfo.second_storage_total > 0 ? (ds.DeviceInfo.second_storage_free / ds.DeviceInfo.second_storage_total * 100).ToString("#.00") + "%" : ""));
                            context.Logger.Text("");
                        }

                        foreach (var ms in context.ManagerList)
                        {
                            context.Logger.Text("PDM - " + ms.ManagerEP.ToString());
                            context.Logger.Text("");
                        }

                        //if (context.ManagerState == null)
                        //{
                        //    logger.Text("PDM:null");
                        //}
                        //else
                        //{
                        //    logger.Text("PDM:" + context.ManagerState.ManagerEP.ToString()
                        //        + "  " + (context.ManagerState.LastHeertBeatTime == null ? "null" : context.ManagerState.LastHeertBeatTime.Value.ToString("yyyy-MM-dd HH:mm:ss")));
                        //}
                        break;
                }

                context.Logger.MC();
                cmd = Console.ReadLine();
            }
        }

        private static void RunAsService(PSDServerContext context)
        {
            Console.WriteLine("PSD Server Running");

            context.Start();

            var cmd = Console.ReadLine();
            while (cmd != "exit")
            {
                cmd = Console.ReadLine();
            }
        }
    }
}
